﻿using System.Linq;
using System.Web.Mvc;
using Financial.Entity;
using Financial.Entity.Enums;
using Financial.Business;
using System.Data.Entity;
using Financial.WebApi.ViewModels;

namespace Financial.WebApi.Controllers
{
    public class IncomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        public JsonResult GetIncomeRecords(IncomeType? sidx, SortOrder sord, int page, int rows)
        {
            var zeroBasedPage = page - 1;
            int totalCount;
            var records = IncomeBL.GetPage(page, rows, out totalCount, sord, sidx).Skip(zeroBasedPage * rows).Take(rows).ToList();
            var extraDay = totalCount % rows == 0 ? 0 : 1;
            var pagesCount = totalCount / rows;
            pagesCount += extraDay;

            var jsonData = new IncomePageVm(page, pagesCount, totalCount, records);

            return Json(jsonData, JsonRequestBehavior.AllowGet);
        }

        public ActionResult Create()
        {
            ViewBag.FamilyMemberId = new SelectList(IncomeBL.AccessFamilyMembers(), "Id", "FullName");

            return View("IncomeDetail");
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(IncomeVm incomeVm)
        {
            Income income = new Income();

            if (ModelState.IsValid)
            {
                income.Id = incomeVm.Id;
                income.FamilyMemberId = incomeVm.FamilyMemberId;
                income.Date = incomeVm.Date.GetValueOrDefault();
                income.Bonus = incomeVm.Bonus;
                income.Wage = incomeVm.Wage;
                income.MealTicket = incomeVm.MealTicket;

                IncomeBL.CreateSaveToDatabase(income);
                return RedirectToAction("Index");
            }

            ViewBag.FamilyMemberId = new SelectList(IncomeBL.AccessFamilyMembers(), "Id", "FullName", income.FamilyMember.Id);

            return View("IncomeDetail", incomeVm);
        }

        public ActionResult Edit(int id)
        {
            Income income = IncomeBL.FindIncome(id);
            IncomeVm incomeVm = new IncomeVm();

            if (income == null)
            {
                return HttpNotFound();
            }

            incomeVm.Bonus = income.Bonus;
            incomeVm.Date = income.Date;
            incomeVm.FamilyMemberId = income.FamilyMemberId;
            incomeVm.MealTicket = income.MealTicket;
            incomeVm.Wage = income.Wage;

            ViewBag.FamilyMemberId = new SelectList(IncomeBL.AccessFamilyMembers(), "Id", "FullName", income.FamilyMemberId);

            return View("IncomeDetail", incomeVm);
        }

        [HttpPost]
        public ActionResult Edit(IncomeVm incomeVm)
        {
            Income income = new Income();            

            if (ModelState.IsValid)
            {
                income.Id = incomeVm.Id;
                income.FamilyMemberId = incomeVm.FamilyMemberId;
                income.Date = incomeVm.Date.GetValueOrDefault();
                income.Bonus = incomeVm.Bonus;
                income.Wage = incomeVm.Wage;
                income.MealTicket = incomeVm.MealTicket;

                IncomeBL.EditSaveToDatabase(income);
                return RedirectToAction("Index");
            }

            ViewBag.FamilyMemberId = new SelectList(IncomeBL.AccessFamilyMembers(), "Id", "FullName", income.FamilyMemberId);

            return View("IncomeDetail", incomeVm);
        }

    }
}